var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		datalist: [],
		pagenum: 1,
		cateArr:[],
		cindex:0,
		isagree:false,
	},
	_onLoad:function(){
		var that = this
		$.get('business/apply',{},function(res){
			if(res.status == 2){
				$.alert(res.msg,function(){
					if(res.tourl){
						$.goto(res.tourl,'redirect');
					}
				});
				return ;
			}
			var clist = res.clist
			var cateArr = [];
			for(var i in clist){
				cateArr.push(clist[i].name)
			}
			var pics = res.info ? res.info.pics : '';
			if(pics){
				pics = pics.split(',');
			}else{
				pics = [];
			}
			var zhengming = res.info ? res.info.zhengming : '';
			if(zhengming){
				zhengming = zhengming.split(',');
			}else{
				zhengming = [];
			}
			res.cateArr = cateArr;
			res.pic = res.info ? [res.info.pic] : [];
			res.pics = pics;
			res.zhengming = zhengming;
			console.log(res)
			that.setData(res)
		});
	},
	cateChange:function(e){
		this.setData({cindex:e.detail.value})
	},
	locationSelect:function(){
		var that = this
		wx.chooseLocation({success:function(res){
			console.log(res)
			that.setData({
				'info.address':res.name,
				'info.latitude':res.latitude,
				'info.longitude':res.longitude
			})
		}})
	},
	subform:function(e){
		var that = this;
		var info = e.detail.value;
		if(info.linkman==''){
			$.error('请填写联系人姓名');return false;
		}
		if(info.linktel==''){
			$.error('请填写联系人电话');return false;
		}
		if(info.tel==''){
			$.error('请填写客服电话');return false;
		}
		if(info.name==''){
			$.error('请填写商家名称');return false;
		}
		if(info.zuobiao==''){
			$.error('请选择店铺坐标');return false;
		}
		if(info.address==''){
			$.error('请填写店铺地址');return false;
		}
		if(info.pic==''){
			$.error('请上传商家主图');return false;
		}
		if(info.pics==''){
			$.error('请上传商家照片');return false;
		}
		if(info.zhengming==''){
			//$.error('请上传证明材料');return false;
		}
		if(info.un==''){
			$.error('请填写登录账号');return false;
		}
		if(info.pwd==''){
			$.error('请填写登录密码');return false;
		}
		var pwd = info.pwd;
		if(pwd.length < 6){
			$.error('密码不能小于6位');return false;
		}
		if(info.repwd!=info.pwd){
			$.error('两次输入密码不一致');return false;
		}
		//if(!/(^0?1[3|4|5|6|7|8|9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$)/.test(tel)){
		//	dialog('手机号格式错误');return false;
		//}
		
		if(that.data.bset.xieyi_show==1 && !that.data.isagree){
			$.error('请先阅读并同意商户入驻协议');return false;
		}
		info.cid = that.data.clist[that.data.cindex].id
		if(that.data.info && that.data.info.id){
			info.id = that.data.info.id;
		}
		$.post("business/apply",{info:info},function(res){
			$.error(res.msg);
			setTimeout(function(){
				$.goto($.indexurl);
			},1000)
		},'提交中');
	},
	isagreeChange:function(e){
		console.log(e.detail.value)
		var val = e.detail.value
		if(val.length > 0){
			this.setData({isagree:true})
		}else{
			this.setData({isagree:false})
		}
	},
	showxieyi:function(){
		this.setData({showxieyi:true})
	},
	hidexieyi:function(){
		this.setData({showxieyi:false,isagree:true})
	},
	setlinkmman:function(e){
		//this.setData({'info.linkman':e.detail.value})	
	},
	setname:function(e){
		this.setData({'info.name':e.detail.value})	
	}
}))